package com.cgl.mapper;

import com.cgl.pojo.Article;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


@Mapper
public interface ArticleMapper {
    @Insert("insert into articles(id,imagePath,title,content,createdTime,uploadTime,updatedTime,status,likeCount,commentCount,viewCount) VALUES (#{id},#{imagePath},#{title},#{content},#{createdTime},#{uploadTime},#{updateTime},#{status},#{likeCount},#{commentCount},#{viewCount})")
    int add(Article article);
    @Select("select * from articles where status=#{status} limit #{pageIndex},#{pageSize}")
    List<Article> selectAll(String status,int pageIndex,int pageSize);
    @Select("select count(*) from articles where status=#{status}")
    int getCount(String status);
    @Select("select * from articles where id=#{id}")
    Article selectById(String id);
    @Select("select * from articles where status=#{status} order by createdTime desc limit #{pageIndex},#{pageSize}")
    List<Article> selectAllByLast(String status,int pageIndex,int pageSize);
    @Update("update articles set viewCount=viewCount+1 where id=#{id}")
    Boolean addviewCount(String id);
    @Update("update articles set likeCount=likeCount+1 where id=#{id}")
    Boolean addLikeCount(String id);
    @Update("update articles set commentCount=commentCount+1 where id=#{id}")
    Boolean addCommentCount(String id);
    @Select("select sum(viewCount) from articles")
    int getAllViewCount();
    @Select("select sum(likeCount) from articles")
    int getAllLikeCount();
    // 获取文章综合排行榜前八
    @Select("select * from articles where status=#{status} order by viewCount desc limit 8")
    List<Article> getArticleRank(String status);
    // 获取文章最新排行榜前八
    @Select("select * from articles where status=#{status} order by createdTime desc limit 8")
    List<Article> getArticleLastRank(String status);
    // 获取文章点赞排行榜前八
    @Select("select * from articles where status=#{status} order by likeCount desc limit 8")
    List<Article> getArticleLikeRank(String status);
    @Select("select * from articles limit #{pageIndex},#{pageSize}")
    List<Article> getAllArticle(int pageIndex,int pageSize);
    @Update("update articles set status=#{status} where id=#{id}")
    int updateStatus(String id,String status);
    @Update("update articles set imagePath=#{imagePath},title=#{title},updatedTime=#{updateTime} where id=#{id}")
    int update(Article article);

}
